package com.project.server.visitorRecord;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.entity.SysUser;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.SecurityUtils;
import com.project.common.utils.poi.ExcelUtil;
import com.project.framework.web.service.TokenService;
import com.project.server.domain.visitorRecord.TVisitorRecord;
import com.project.server.service.visitorRecord.ITVisitorRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 访客记录信息
 *
 * @author project
 */
@RestController
@RequestMapping("/server/visitor")
public class TVisitorRecordController extends BaseController {

    @Resource
    private ITVisitorRecordService visitorRecordService;

    @Resource
    private TokenService tokenService;

    /**
     * 获取访客记录信息列表
     */
    @PreAuthorize("@ss.hasPermi('server:visitor:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVisitorRecord visitorRecord){
        startPage();
        List<TVisitorRecord> list = visitorRecordService.selectVisitorRecordList(visitorRecord);
        return getDataTable(list);
    }

    /**
     * 导出访客记录列表
     */
    @Log(title = "访客记录管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('server:visitor:export')")
    @GetMapping("/export")
    public AjaxResult export(TVisitorRecord visitorRecord){
        List<TVisitorRecord> list = visitorRecordService.selectVisitorRecordList(visitorRecord);
        ExcelUtil<TVisitorRecord> util = new ExcelUtil<TVisitorRecord>(TVisitorRecord.class);
        return util.exportExcel(list,"访客记录数据");
    }

    /**
     * 根据访客id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('server:visitor:query')")
    @GetMapping(value = "/{visitId}")
    public AjaxResult getInfo(@PathVariable Long visitId) {
        return AjaxResult.success(visitorRecordService.selectVisitorRecordByVisitId(visitId));
    }

    /**
     * 新增访客记录
     */
    @PreAuthorize("@ss.hasPermi('server:visitor:add')")
    @Log(title = "访客记录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TVisitorRecord visitorRecord){
        visitorRecord.setCreateBy(SecurityUtils.getUsername());
        return toAjax(visitorRecordService.insertVisitorRecord(visitorRecord));
    }

    /**
     * 修改访客记录
     */
    @PreAuthorize("@ss.hasPermi('server:visitor:edit')")
    @Log(title = "访客记录管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody TVisitorRecord visitorRecord){
        visitorRecord.setUpdateBy(SecurityUtils.getUsername());
        SecurityUtils.getLoginUser().getUser();
        return toAjax(visitorRecordService.updateVisitorRecord(visitorRecord));
    }

    /**
     * 审批访客记录
     */
    @PreAuthorize("@ss.hasPermi('server:visitor:changeState')")
    @Log(title = "访客记录管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeState")
    public AjaxResult changeState(@Validated @RequestBody TVisitorRecord visitorRecord){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        visitorRecord.setApproverId(user.getUserId());
        return toAjax(visitorRecordService.updateVisitorRecordApproverState(visitorRecord));
    }

    /**
     * 删除访客记录
     */
    @PreAuthorize("@ss.hasPermi('server:visitor:remove')")
    @Log(title = "访客记录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{visitIds}")
    public AjaxResult remove(@PathVariable Long[] visitIds){
        return toAjax(visitorRecordService.deleteVisitorRecordByVisitIds(visitIds));
    }



}
